package algocasts;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class P35_InorderTraversal {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
    }
    public List<Integer> inorderTraversal_Recursive(TreeNode root) {
        if (root == null) return new ArrayList<>();
        List<Integer> left = inorderTraversal_Recursive(root.left);
        List<Integer> right = inorderTraversal_Recursive(root.right);
        left.add(root.val);
        left.addAll(right);
        return left;
    }

    public List<Integer> inorderTraversal_Iterative(TreeNode root) {
        Stack<TreeNode> s = new Stack<>();
        List<Integer> res = new ArrayList<>();
        while (root != null || !s.isEmpty()) {
            while (root != null) {
                s.push(root);
                root = root.left;
            }
            root = s.pop();
            res.add(root.val);
            root = root.right;
        }
        return res;
    }
}
